package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Address;
import com.hemaapp.yjnh.utils.RegexUtils;
import com.hemaapp.yjnh.view.BaseButtonDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;

public class AddressModifyActivity extends BaseActivity implements OnClickListener {

    private static final int REQUEST_PROVINCE = 0x103;
    private static final int REQUEST_CITY = 0x104;
    private static final int REQUEST_DISTRICT = 0x105;

    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.edt_name)
    EditText edtName;
    @Bind(R.id.edt_mobile)
    EditText edtMobile;
    @Bind(R.id.tv_province)
    TextView tvProvince;
    @Bind(R.id.tv_city)
    TextView tvCity;
    @Bind(R.id.tv_region)
    TextView tvRegion;
    @Bind(R.id.edt_zipcode)
    EditText edtZipcode;
    @Bind(R.id.edt_address)
    EditText edtAddress;

    private Address address;
    private String provinceId = "", cityId = "", regionId = "";
    private String id = "0";//地址的主键id，新增时为0
    private int btnColor;//弹出dialog的button的颜色

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_addressmodify);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        btnColor = getResources().getColor(R.color.theme_orange);
        if (address != null) {
            id = address.getId();
            edtName.setText(address.getName());
            edtMobile.setText(address.getTel());
            String[] str = address.getNamepath().split(",");
            tvProvince.setText(str[0]);
            tvCity.setText(str[1]);
            tvRegion.setText(str[2]);
            edtZipcode.setText(address.getZipcode());
            edtAddress.setText(address.getAddress());
            provinceId = address.getProvince_id();
            cityId = address.getCity_id();
            regionId = address.getDistrict_id();
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ADDRESS_SAVE:
                showProgressDialog("请稍后");
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ADDRESS_SAVE:
                cancelProgressDialog();
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ADDRESS_SAVE:
                showTextDialog("收货地址保存成功");
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        setResult(RESULT_OK);
                        finish();
                    }
                }, 1000);
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ADDRESS_SAVE:
                XtomToastUtil.showShortToast(mContext, "保存失败，"+baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ADDRESS_SAVE:
                XtomToastUtil.showShortToast(mContext, "保存失败，请稍后再试");
                break;
        }
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
        address = (Address) mIntent.getSerializableExtra("address");
    }

    @Override
    protected void setListener() {
        titleText.setText("地址编辑");
        titleRightBtn.setText("保存");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK != resultCode) {
            return;
        }
        switch (requestCode) {
            case REQUEST_PROVINCE:
                if (!getCityId().equals(provinceId)) {
                    cityId = "";
                    regionId = "";
                    tvCity.setText("");
                    tvRegion.setText("");
                }
                provinceId = getCityId();
                tvProvince.setText(getCityName());
                break;
            case REQUEST_CITY:
                if (!getCityId().equals(cityId)) {
                    regionId = "";
                    tvRegion.setText("");
                }
                cityId = getCityId();
                tvCity.setText(getCityName());
                break;
            case REQUEST_DISTRICT:
                regionId = getCityId();
                tvRegion.setText(getCityName());
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.title_left_btn, R.id.title_right_btn, R.id.layout_province,
            R.id.layout_city, R.id.layout_region})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_btn:
                String nicknameStr = edtName.getText().toString().trim();
                String mobileStr = edtMobile.getText().toString().trim();
                String provinceStr = tvProvince.getText().toString().trim();
                String cityStr = tvCity.getText().toString().trim();
                String regionStr = tvRegion.getText().toString().trim();
                String zipcodeStr = edtZipcode.getText().toString().trim();
                String addressStr = edtAddress.getText().toString().trim();
                if (isNull(nicknameStr)) {
                    XtomToastUtil.showShortToast(mContext, "请填写收件人姓名");
                    break;
                }
                if (isNull(mobileStr)) {
                    XtomToastUtil.showShortToast(mContext, "请填写电话号码");
                    break;
                }
                if (mobileStr.length() < 11) {//这里有固话有手机号，固话有可能是11位或者12位。所以只判断位数
                    XtomToastUtil.showShortToast(mContext, "请输入有效的固话或者手机号");
                    break;
                }
                if (isNull(provinceStr)) {
                    XtomToastUtil.showShortToast(mContext, "请选择省份");
                    break;
                }
                if (isNull(cityStr)) {
                    XtomToastUtil.showShortToast(mContext, "请选择地级市");
                    break;
                }
                if (isNull(regionStr)) {
                    XtomToastUtil.showShortToast(mContext, "请选择县区");
                    break;
                }
                if (isNull(zipcodeStr)) {
                    XtomToastUtil.showShortToast(mContext, "请填写邮编");
                    break;
                }
                if (isNull(addressStr)) {
                    XtomToastUtil.showShortToast(mContext, "请填写详细地址");
                    break;
                }
                //调取网络接口保存地址
                getNetWorker().addressSave(getApplicationContext().getUser().getToken(),
                        id, nicknameStr, mobileStr, provinceId, cityId, regionId, addressStr, zipcodeStr);
                break;
            case R.id.layout_province:
                Intent itProvince = new Intent(mContext, CityActivity.class);
                itProvince.putExtra("type", 2);
                itProvince.putExtra("parent_id", "0");
                startActivityForResult(itProvince, REQUEST_PROVINCE);
                break;
            case R.id.layout_city:
                if (isNull(provinceId)) {
                    XtomToastUtil.showShortToast(mContext, "请选择省份");
                    break;
                }
                Intent itCity = new Intent(mContext, CityActivity.class);
                itCity.putExtra("type", 3);
                itCity.putExtra("parent_id", provinceId);
                startActivityForResult(itCity, REQUEST_CITY);
                break;
            case R.id.layout_region:
                if (isNull(cityId)) {
                    XtomToastUtil.showShortToast(mContext, "请选择省份");
                    break;
                }
                Intent itRegion = new Intent(mContext, CityActivity.class);
                itRegion.putExtra("type", 4);
                itRegion.putExtra("parent_id", cityId);
                startActivityForResult(itRegion, REQUEST_DISTRICT);
                break;
        }
    }
}
